import java.util.*;

public class ReaderWriter {
	class CallmeWrite {
		void callwrite(String msg) {
       	 	FileWriter fw = new FileWriter("file.out");	
			BufferedWriter bw = new BufferedWriter(fw);
            bw.write(msg);
			try {
         		Thread.sleep(1000);
     		} catch (InterruptedException e) {
        		System.out.println("Interrupted");
   			}
            bw.newLine();
            bw.close();
            fw.close();
		}
	}

	class Caller implements Runnable {
        CallmeWrite writer;
		String msg;
		Thread t;
        public Caller(CallmeWrite wrt, String s) {
			writer = wrt;
            msg = s;
            t = new Thread(this);
            t.start();
		}
        public void run() {
			synchronized(writer) {
				writer.callwrite(msg);
			}
		}
	}

	public static void main(String args[]) {
        CallmeWrite writer = new CallmeWrite();
        Caller ob1 = new Caller(writer, "Hello");
        Caller ob2 = new Caller(writer, "over");
        Caller ob3 = new Caller(writer, "there");
        Caller ob4 = new Caller(writer, "world");

       return;
    }
}
